<?php

class company extends CActiveRecord {
/**
 * The followings are the available columns in table 'customer':
 * @var string $cname
 * @var string $address
 * @var string $tele
 * @var string $fax
 * @var string $pic
 * @var integer $id
 * @var string $email
 * @var integer $postal
 * @var integer $date_added
 * @var integer $country_code
 */
    public $transaction = null;
    public $address,$members;
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'company';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
        array('cname','length','max'=>250),
        array('cname', 'unique','allowEmpty'=>false,'caseSensitive'=>false),
        array('cname','required'),
        //array('id','numerical', 'integerOnly'=>true),
        );
    }
    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'telephone'=>array(self::HAS_MANY,'company_contacts','contact_id','condition'=>'type<10'),
            'fax'=>array(self::HAS_MANY,'company_contacts','contact_id','condition'=>'type>=10'),
            'members'=>array(self::HAS_MANY,'company_members','cust_id'),
            'warehouse'=>array(self::HAS_MANY, 'company_warehouse', 'contact_id'),
        );
    }
    /**
     * @return array customized attribute labels (name=>label)
     */

    public function attributeLabels() {
        return array(
            'cname'=>'Company',
        );
    }
    public function getName($companyID) {
        $this->findByPk($companyID);
    }
    public function saveCompany() {
    // Vallidate all informations
        $validate = true;
        foreach($this->address as $address) {
            if(!$address->validateall()) {
                $validate = false;
            }
        }
        foreach($this->members as $item) {
            if(!$item->validateall()) {
                $validate = false;
            }
        }
        if(!$this->validate()) {
            $validate = false;
        }
        if(!$validate) {
            Yii::log("Main validating error",'error','application.company');
            return false;
        }
        // end validate
        if($this->save(false)) {
            foreach($this->address as $address) {
                $address->company_id = $this->id;
                $address->saveaddress(false);
            }
            foreach($this->members as $item) {
                $item->company_id = $this->id;
                $item->savemembers(false);
            }
        }
        else
            return false;
        return true;
    }
}